package com.acwing.lc.part1;

import java.util.Arrays;

/**
 * @author rkc
 * @date 2022/9/12 15:22
 */
public class LC36有效的数独 {

    public boolean isValidSudoku(char[][] board) {
        boolean[] st = new boolean[9];
        for (int i = 0; i < 9; i++) {
            Arrays.fill(st, false);
            for (int j = 0; j < 9; j++) {
                if (board[i][j] == '.') continue;
                if (st[board[i][j] - '1']) return false;
                st[board[i][j] - '1'] = true;
            }
        }
        for (int i = 0; i < 9; i++) {
            Arrays.fill(st, false);
            for (int j = 0; j < 9; j++) {
                if (board[j][i] == '.') continue;
                if (st[board[j][i] - '1']) return false;
                st[board[j][i] - '1'] = true;
            }
        }
        for (int i = 0; i < 9; i += 3) {
            for (int j = 0; j < 9; j += 3) {
                Arrays.fill(st, false);
                for (int x = 0; x < 3; x++) {
                    for (int y = 0; y < 3; y++) {
                        if (board[i + x][j + y] == '.') continue;
                        if (st[board[i + x][j + y] - '1']) return false;
                        st[board[i + x][j + y] - '1'] = true;
                    }
                }
            }
        }
        return true;
    }
}
